/* ----------------------------------------------------------------------
*
* Copyright (C) 2025 XiaoFei. All rights reserved.
*
* @file:		XiaoFei_Uart4.c
* @author:		XiaoFei
* @Date:     	13-January-2025
* @Revision: 	V1
* @Project: 	XiaoFei BSP Library
*
* -------------------------------------------------------------------- */

#include "stm32f4xx.h"
#include "XiaoFei_Uart4.h"
#include "stdio.h"
#include "stdarg.h"

/* 
* @function: 	Uart4_Init
*/
void Uart4_Init(void)
{
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStructure.GPIO_Speed=GPIO_High_Speed;
	GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Pin=Uart4_Pin_TX;
	GPIO_Init(Uart4_GPIO_TX,&GPIO_InitStructure);
	#ifdef Uart4_AddRx
		GPIO_InitStructure.GPIO_Pin=Uart4_Pin_RX;
		GPIO_Init(Uart4_GPIO_RX,&GPIO_InitStructure);
	#endif
		GPIO_PinAFConfig(Uart4_GPIO_TX,GPIO_PinSource0,GPIO_AF_UART4);
    #ifdef Uart4_AddRx
        GPIO_PinAFConfig(Uart4_GPIO_RX,GPIO_PinSource1,GPIO_AF_UART4);
    #endif
	USART_InitTypeDef USART_Initstructure;
	USART_Initstructure.USART_BaudRate=Uart4_BaudRate;
	USART_Initstructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    #ifdef Uart4_AddRx
        USART_Initstructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
    #else
        USART_Initstructure.USART_Mode=USART_Mode_Tx;
    #endif
	USART_Initstructure.USART_Parity=USART_Parity_No;
	USART_Initstructure.USART_StopBits=USART_StopBits_1;
	USART_Initstructure.USART_WordLength=USART_WordLength_8b;
	USART_Init(UART4,&USART_Initstructure);
    #ifdef Uart4_AddRx
        NVIC_PriorityGroupConfig(Uart4_Group);
        NVIC_InitTypeDef NVIC_InitStructure;
        NVIC_InitStructure.NVIC_IRQChannel=UART4_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=Uart4_ITPre;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority=Uart4_ITSub;
        NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
        NVIC_Init(&NVIC_InitStructure);
	    USART_ITConfig(UART4,USART_IT_RXNE,ENABLE);
    #endif
	USART_Cmd(UART4,ENABLE);
}

/* 
* @function: 	Uart4_SendByte
* @parameter:	[in] Byte					 The byte will be send.
*/
void Uart4_SendByte(unsigned char Byte)
{
    USART_SendData(UART4,Byte);
    while(USART_GetFlagStatus(UART4,USART_FLAG_TXE)==RESET);
}

/* 
* @function: 	Uart4_SendString
* @parameter:	[in] String					 The String will be send.
*/
static void Uart4_SendString(char *String)
{
	for (unsigned int i=0; String[i]!='\0';i ++)
		Uart4_SendByte(String[i]);
}

/* 
* @function: 	Uart4_Printf
* @parameter:	[in] format					 The parameter will be send in Uart4_Printf.
*/
void Uart4_Printf(char *format, ...)
{
	char String[100];
	va_list arg;
	va_start(arg, format);
	vsprintf(String, format, arg);
	va_end(arg);
	Uart4_SendString(String);
}

#ifdef Uart4_AddRx
/* 
* @function: 	UART4_IRQHandler
* @callback:	The function will be callback when the Uart4 receive the data.
*/
void UART4_IRQHandler(void)
{
	if (USART_GetITStatus(UART4, USART_IT_RXNE) == SET)
		USART_ClearITPendingBit(UART4, USART_IT_RXNE);
}
#endif


